﻿using UnityEngine;

namespace IQIGame.Onigao.GameAOT
{
    public static class HUDUtil
    {
        // 功能：执行数字的动画效果
        public static void PlayAnimation(HUDNumberEntry pNode, bool bFirst, HUDNumberSetting attrib, bool bPause, float fBaseScale , 
            float rRenderCurrentDuration, float fScreenScaleX, float fScreenScaleY, float fDurationTime)
        {
            float currentDuration = (Time.time - pNode.m_fStartTime);
            if (bPause)
                currentDuration = rRenderCurrentDuration;
            float fAlpha = attrib.AlphaCurve.Evaluate(currentDuration); //淡出效果;
            float fScale = attrib.ScaleCurve.Evaluate(currentDuration) * fBaseScale; //变大效果;
            Vector2 dir = pNode.m_ScreenDir;
            float fXPos = attrib.MoveXCurve.Evaluate(currentDuration); //X轴抖动效果;
            float fYPos = attrib.MoveYCurve.Evaluate(currentDuration); //Y轴移动效果;
            float fOldAlpha = pNode.m_fAlpha;
            float fOldScale = pNode.m_fAniScale;
            float fOldMoveX = pNode.m_vMove.x;
            float fOldMoveY = pNode.m_vMove.y;
            pNode.m_fAlpha = fAlpha;
            pNode.m_fAniScale = fScale;
            if (attrib.ScreenAlign)
            {
                pNode.m_vMove.x = 0.0f;
                pNode.m_vMove.y = fYPos * fScreenScaleY;
            }
            else
            {
                pNode.m_vMove.x = (attrib.OffsetX + fXPos * pNode.m_vMoveDis.x) * fScreenScaleX;
                pNode.m_vMove.y = (attrib.OffsetY + fYPos * pNode.m_vMoveDis.y) * fScreenScaleY;
            }

            pNode.m_bStop = currentDuration > fDurationTime;
            if (bPause)
                pNode.m_bStop = false;

            int nAlpha = (int) (fAlpha * 255.0f + 0.5f);
            if (nAlpha < 0)
                nAlpha = 0;
            if (nAlpha > 255)
                nAlpha = 255;
            byte alpha = (byte) nAlpha;

            bool bDirty;
            if (!bFirst)
                bDirty = pNode.UpdateScreenPos(attrib);
            else
                bDirty = true;

            if (!bDirty)
            {
                if (Mathf.Abs(fOldAlpha - pNode.m_fAlpha) > 0.0001f)
                    bDirty = true;
                if (!bDirty && Mathf.Abs(fOldScale - pNode.m_fScale) > 0.0001f)
                    bDirty = true;
                if (!bDirty && Mathf.Abs(fOldMoveX - pNode.m_vMove.x) > 0.0001f)
                    bDirty = true;
                if (!bDirty && Mathf.Abs(fOldMoveY - pNode.m_vMove.y) > 0.0001f)
                    bDirty = true;
            }

            if (!bDirty)
                return;

            fScale *= pNode.m_fScale;

            // 更新顶点数据
            // Vector2 vScreenPos = pNode.m_vScreenPos;
            Vector2 vMove = pNode.m_vMove;
            for (int i = pNode.m_aSprite.size - 1; i >= 0; --i)
            {
                HUDVertex v = pNode.m_aSprite[i];
                v.Move = vMove;
                v.WorldPos = pNode.m_vPos;
                // v.ScreenPos = vScreenPos;
                v.Scale = fScale;
                v.clrLU.a = alpha;
                v.clrLD.a = alpha;
                v.clrRU.a = alpha;
                v.clrRD.a = alpha;
                v.hudMesh.VertexDirty();
            }
        }
    }
}